package com.xiaoxin.yixinai.role;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoxin.yixinai._frame.base.BaseRepository;
import com.xiaoxin.yixinai._frame.base.BaseServiceProxy;


@Transactional
@Service
public class RoleUserService extends BaseServiceProxy<RoleUser> {

	@Autowired
	private RoleUserDao roleUserDao;
	
	@Override
	protected BaseRepository<RoleUser> getBaseRepository() {
		return this.roleUserDao;
	}

	/**
	 *
	 * 功能：保存角色成员
	 * @param roleIdArr 角色数组
	 * @param userId 用户id
	 * @param companyId 公司id
	 * @param type 类型1主角色 ,类型0辅助角色
	 * @param isDelete
	 */
	public void saveRoleUsersByRoleIds(Integer[] roleIdArr,Integer userId, Integer companyId,Integer type,Boolean isDelete){
			if(isDelete){
				// 对应角色成员全部删除
				roleUserDao.deleteRolesByUserId(userId,type);
			}
			if (null != roleIdArr){
			    for (int i = 0; i < roleIdArr.length; i++) {
					RoleUser roleUser = new RoleUser();
					roleUser.setRoleId(roleIdArr[i]);
					roleUser.setUserId(userId);
					roleUser.setCompanyId(companyId);
					roleUser.setType(type);
					roleUserDao.save(roleUser);
                }
			}
	}

	public void deleteRoleUserByUserIds(List<Integer> userIds) {
		roleUserDao.deleteRoleUsersByUserId(userIds);
	}
	
	public RoleUser findByUserId(Integer userId) {
		return roleUserDao.findByUserId(userId);
	}
}
